﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using CarolLib;
using CarolLib.Control;
using MiniCMS.Configuration;
using MiniCMS.Business;

namespace MiniCMS.UserControls
{
    [ModuleContract("2E15D644-B16F-4AA0-8142-65546BCC34F1", "系统设置(&M)", "修改密码")]
    public partial class UCChangePassword : MdiUserControl
    {
        #region Ctor.

        public UCChangePassword()
            : base()
        {
            InitializeComponent();
        }

        public UCChangePassword(XmlNode node)
            : this()
        {
            InitializeComponent();
        }

        #endregion

        private void btnChangePW_Click(object sender, EventArgs e)
        {
            if (this.tbNewPW.Text.Trim() == string.Empty
                ||
                this.tbNewPW.Text.Trim() != this.tbNewPWConfirm.Text.Trim()
                ||
                AuthUserContext.Encrypt(this.tbOldPW.Text.Trim()) != AuthUserContext.Instance.User.Password)
            {
                MessageBox.Show("原密码错误或新密码输入不一致！");
                return;
            }

            AuthUserContext.Instance.User.Password = AuthUserContext.Encrypt(this.tbNewPWConfirm.Text);
            OleDbDataProvider.UpdateSystemUser(AuthUserContext.Instance.User);

            MessageBox.Show("密码修改成功！");

            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                    (item as TextBox).Text = string.Empty;
            }
        }
    }
}
